package zhihui.mobile.ui.register;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zgm.utils.UiUtils;

import java.util.Timer;
import java.util.TimerTask;

import zhihui.mobile.R;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.register.IPhoneCodeContract;
import zhihui.mobile.presenter.register.IRegisterContract;
import zhihui.mobile.presenter.register.PhoneCodePresenter;
import zhihui.mobile.ui.BaseFragment;

/**
 * Created by 张公铭 on 2017/7/11 0011.
 */

public class PhoneCodeFragment extends BaseFragment implements IPhoneCodeContract.IPhoneCodeView
{
    private IPhoneCodeContract.IPhoneCodePresenter codePresenter = new PhoneCodePresenter();
    private IRegisterContract.IRegisterPersenter registerPersenter;

    private EditText ed_code;
    private Button btn_msg;
    private Button btn_next;
    private TextView tv_msg;
    private Timer mTime = new Timer();
    private Handler mHandler;

    private final int UPDATE_TIME = 1;
    private final int STOP_TIMER = 2;

    public void setRegisterPersenter(IRegisterContract.IRegisterPersenter registerPersenter)
    {
        this.registerPersenter = registerPersenter;
    }

    @Override
    public void initView()
    {
        mHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                switch (msg.what)
                {
                    case UPDATE_TIME:
                        btn_msg.setText(msg.arg1 + "S");
                        break;
                    case STOP_TIMER:
                        stopTimer();
                        btn_msg.setText("获取验证码");
                        btn_msg.setEnabled(true);
                        break;
                }
            }
        };

        ed_code = (EditText) mView.findViewById(R.id.ed_register_phone_code);
        UiUtils.editTextFocus(ed_code);
        btn_msg = (Button) mView.findViewById(R.id.btn_register_get_code);
        btn_msg.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                codePresenter.getMsg();
            }
        });

        btn_next = (Button) mView.findViewById(R.id.btn_register_phone_code_next);
        btn_next.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                tv_msg.setText("");
                codePresenter.goPasswordFragment(ed_code.getText().toString());
            }
        });

        tv_msg = (TextView) mView.findViewById(R.id.tv_phone_code_msg);
        tv_msg.setText("");
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.register_code_fragment;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{codePresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        codePresenter.init(this, getContext());
    }

    private void stopTimer()
    {
        if (mTime != null)
        {
            mTime.cancel();
            mTime = null;
        }
    }

    @Override
    public void startTimer()
    {
        btn_msg.setEnabled(false);
        if (mTime == null)
        {
            mTime = new Timer();
        }
        mTime.schedule(new TimerTask()
        {
            int lastTime = 60;
            @Override
            public void run()
            {
                Message message = new Message();
                lastTime--;
                if (lastTime == 0)
                {
                    message.what = STOP_TIMER;
                } else
                {
                    message.what = UPDATE_TIME;
                    message.arg1 = lastTime;
                }
                mHandler.sendMessage(message);
            }
        }, 0, 1000);
    }

    @Override
    public void goPasswordFragment()
    {
        registerPersenter.showPasswordFragment();
    }

    @Override
    public void updateCodeText(String code)
    {
        ed_code.setText(code);
    }

    @Override
    public void showMessage(String message)
    {
        tv_msg.setText(message);
    }
}
